_G.current_round = {}
_G.last_round = {}
_G.last_2nd_round = {}
_G.last_3rd_round = {}
-- _G.counterpart = {}
_G.counterpart = {-1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0}
-- _G.counterpart = {0, 0, 0, 0}

-- _G.teamid2hero ={1,2,3,4,5,6,7,8}
-- function TeamId2Hero(id)
-- 	if id == nil then
-- 		return nil
-- 	else
-- 		return _G.teamid2hero[id]
-- 	end
-- end

-- 【底层方法】
function IsValueInTable(value, table)
    for k, v in pairs(table) do
        if v == value then
            return true
        end
    end
    return false
end

function AllocateABattleRoundV3()

    local finished = false
    local trytime = 0

    _G.last_3rd_round = _G.last_2nd_round
    _G.last_2nd_round = _G.last_round
    _G.last_round = _G.current_round

    while trytime < 10000 and finished == false do
        trytime = trytime + 1
        _G.current_round = {}

        local alive_player_count = 0
        -- 统计玩家死活
        for u, v in pairs(_G.counterpart) do
            if _G.counterpart[u] ~= -1 then
                -- 活玩家
                _G.counterpart[u] = u
                alive_player_count = alive_player_count + 1
            else
                -- 死玩家
                _G.counterpart[u] = -1
            end
        end

        local rann = 0
        if alive_player_count <= 1 then
            rann = 0
        elseif alive_player_count == 2 then
            rann = 1
        else
            rann = math.random(1, alive_player_count - 1)
        end
        -- 给活玩家分配一个随机对手
        for rotate_count = 1, rann do
            for i, j in pairs(_G.counterpart) do
                if j ~= -1 then
                    local aliveteam = j

                    local try_count = 0
                    while (aliveteam == j or _G.counterpart[aliveteam] == nil or _G.counterpart[aliveteam] == -1) and
                        try_count < 10000 do
                        aliveteam = aliveteam + 1
                        if aliveteam > 13 then
                            aliveteam = 6
                        end
                        try_count = try_count + 1
                    end
                    _G.counterpart[i] = aliveteam
                end
            end
        end

        for ii, jj in pairs(_G.counterpart) do
            if jj ~= -1 then
                table.insert(_G.current_round, ii * 100 + jj)
            end
        end

        finished = true
        for i, v in pairs(_G.current_round) do
            if alive_player_count >= 7 then
                if IsValueInTable(v, _G.last_round) == true or IsValueInTable(v, _G.last_2nd_round) == true or
                    IsValueInTable(v, _G.last_3rd_round) == true then
                    finished = false
                end
            elseif alive_player_count >= 5 then
                if IsValueInTable(v, _G.last_round) == true or IsValueInTable(v, _G.last_2nd_round) == true then
                    finished = false
                end
            elseif alive_player_count >= 3 then
                if IsValueInTable(v, _G.last_round) == true then
                    finished = false
                end
            elseif alive_player_count >= 1 then
                finished = true
            else
                return
            end
        end
    end

end

function result(teamid)
    local opp = -1
    local my_opp = -1
    for myteam, enemyteam in pairs(_G.counterpart) do
        if enemyteam == teamid then
            opp = myteam
        end
        if myteam == teamid then
            my_opp = enemyteam -- my_opp是teamid要打的敌人队伍
        end
    end
    print("teamid:" .. teamid .. " opp:" .. opp .. " my_opp:" .. my_opp)
end

function pt(n, t, start)
    if t == nil then
        print(nil)
        return
    end

    local s = n .. ":{"

    for k = start, #t do
        local v = t[k]
        s = s .. k .. ":" .. v .. ","
    end
    s = string.sub(s, 0, string.len(s) - 1)
    s = s .. "}"
    print(s)

end

function func1()
    AllocateABattleRoundV3()
    -- pt("current_round",_G.current_round)
    pt("counterpart", _G.counterpart)
    for k, v in pairs(_G.counterpart) do
        result(k)
    end
end

function func2()
    for i = 1, 10 do
        AllocateABattleRoundV3()
        pt("counterpart", _G.counterpart, 6)
    end
end


function duang1(v_hp)
    local v_max_hp = 100
    local v_hp_per = 1.0 * v_hp / v_max_hp
    local duang_check_prob = 0
    if math.tan(v_hp_per * 3) > 0 and v_hp_per < 0.5 then
        duang_check_prob = math.pow(1 / math.tan(v_hp_per * 2.8), 1 / 3) * (-math.log(v_hp_per)) / 25
    end
    local p = 1.25
    local duang_per = 0.15
    -- 幸运挂坠的加成
    duang_check_prob = duang_check_prob * p
    return duang_check_prob, duang_per * p
end



function duangTest()
    local p1,p2
    for i = 1, 100 do
        p1,p2 = duang1(i)
        print("duang:"..i.." "..p1.." "..p2)
    end

end


function eggAdd(egg_chess_count)
    local pp_cal = (egg_chess_count)*0.02+0.475
    return pp_cal;
end

function eggTest()
    local ps = 1    
    local p1
    for i = 1, 8 do
        p1 = eggAdd(i)
        ps=ps*p1
        print("egg:"..i.." "..p1.." "..ps)
    end

end

eggTest()
